package com.myhkvideo.yulongbpcdemo.service;

import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.TimerTask;
@Service
public class DataExportService {

    // 数据库连接信息
    private static final String DB_URL = "jdbc:sqlserver://192.168.5.211:1433;databaseName=yulongProductData";
    private static final String DB_USER = "sa";
    private static final String DB_PASSWORD = "Dfmc@123";

    // 存放文件夹的路径，可修改
    private static String folderPath = "D:/Users/15954/Desktop";
    // 文件夹的名称，可修改
    private static String folderName = "数据文件";


    public static class DataExportTimerTask extends TimerTask {
        @Override
        public void run() {
            try {
                // 建立数据库连接
                Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
                Statement statement = connection.createStatement();

                // 查询数据库中的数据
                String sql = "SELECT * FROM DayProductData";
                ResultSet resultSet = statement.executeQuery(sql);

                // 处理查询结果
                while (resultSet.next()) {
                    // 获取日期
                    Date createTime = resultSet.getDate("时间");
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
                    String dateString = dateFormat.format(createTime);

                    // 构建文件路径和文件名
                    String folderFullPath = folderPath + File.separator + folderName;
                    File folder = new File(folderFullPath);
                    if (!folder.exists()) {
                        folder.mkdirs(); // 如果文件夹不存在，则创建
                    }
                    String filePath = folderFullPath + File.separator + dateString +"生产数据"+ ".txt";

                    // 写入数据到文件
                    try (FileWriter writer = new FileWriter(filePath)) {
                        writer.write(dateString + "：");
                        writer.write("一车间出矿量" + resultSet.getDouble("一车间出矿量") + "，");
                        writer.write("二车间出矿量" + resultSet.getDouble("二车间出矿量") + "，");
                        writer.write("冶炼厂出矿量" + resultSet.getDouble("冶炼厂出矿量") + "，");
                        writer.write("总出矿量" + resultSet.getDouble("总出矿量") + "，");
                        writer.write("总剥岩量" + resultSet.getDouble("总剥岩量") + "，");
                        writer.write("一车间原矿处理量" + resultSet.getDouble("一车间原矿处理量") + "，");
                        writer.write("二车间原矿处理量" + resultSet.getDouble("二车间原矿处理量") + "，");
                        writer.write("冶炼厂原矿处理量" + resultSet.getDouble("冶炼厂原矿处理量") + "，");
                        writer.write("总原矿处理量" + resultSet.getDouble("总原矿处理量") + "，");
                        writer.write("一车间铜金属量" + resultSet.getDouble("一车间铜金属量") + "，");
                        writer.write("一车间钼金属量" + resultSet.getDouble("一车间钼金属量") + "，");
                        writer.write("二车间铜金属量" + resultSet.getDouble("二车间铜金属量") + "，");
                        writer.write("二车间钼金属量" + resultSet.getDouble("二车间钼金属量") + "，");
                        writer.write("冶炼厂金属量" + resultSet.getDouble("冶炼厂金属量") + "，");
                        writer.write("总铜金属量" + resultSet.getDouble("总铜金属量") + "，");
                        writer.write("总钼金属量" + resultSet.getDouble("总钼金属量") + "，");
                        writer.write("一车间铜精矿品位" + resultSet.getDouble("一车间铜精矿品位") + "，");
                        writer.write("一车间钼精矿品位" + resultSet.getDouble("一车间钼精矿品位") + "，");
                        writer.write("二车间铜精矿品位" + resultSet.getDouble("二车间铜精矿品位") + "，");
                        writer.write("二车间钼精矿品位" + resultSet.getDouble("二车间钼精矿品位") + "，");
                        writer.write("一车间铜回收率" + resultSet.getDouble("一车间铜回收率") + "，");
                        writer.write("一车间钼回收率" + resultSet.getDouble("一车间钼回收率") + "，");
                        writer.write("二车间铜回收率" + resultSet.getDouble("二车间铜回收率") + "，");
                        writer.write("二车间钼回收率" + resultSet.getDouble("二车间钼回收率") + "，");
                        writer.write("冶炼厂回收率" + resultSet.getDouble("冶炼厂回收率") + "，");
                        writer.write("一车间原矿品位" + resultSet.getDouble("一车间原矿品位") + "，");
                        writer.write("二车间原矿品位" + resultSet.getDouble("二车间原矿品位") + "，");
                        writer.write("冶炼厂原矿品位" + resultSet.getDouble("冶炼厂原矿品位") + "，");
                        writer.write("一车间1#磨机处理量" + resultSet.getDouble("一车间1#磨机处理量") + "，");
                        writer.write("一车间1#磨机运行时长" + resultSet.getDouble("一车间1#磨机运行时长") + "，");
                        writer.write("一车间1#磨机台效" + resultSet.getDouble("一车间1#磨机台效") + "，");
                        writer.write("一车间1#磨机运转率" + resultSet.getDouble("一车间1#磨机运转率") + "，");
                        writer.write("一车间2#磨机处理量" + resultSet.getDouble("一车间2#磨机处理量") + "，");
                        writer.write("一车间2#磨机运行时长" + resultSet.getDouble("一车间2#磨机运行时长") + "，");
                        writer.write("一车间2#磨机台效" + resultSet.getDouble("一车间2#磨机台效") + "，");
                        writer.write("一车间2#磨机运转率" + resultSet.getDouble("一车间2#磨机运转率") + "，");
                        writer.write("一车间3#磨机处理量" + resultSet.getDouble("一车间3#磨机处理量") + "，");
                        writer.write("一车间3#磨机运行时长" + resultSet.getDouble("一车间3#磨机运行时长") + "，");
                        writer.write("一车间3#磨机台效" + resultSet.getDouble("一车间3#磨机台效") + "，");
                        writer.write("一车间3#磨机运转率" + resultSet.getDouble("一车间3#磨机运转率") + "，");
                        writer.write("一车间4#磨机处理量" + resultSet.getDouble("一车间4#磨机处理量") + "，");
                        writer.write("一车间4#磨机运行时长" + resultSet.getDouble("一车间4#磨机运行时长") + "，");
                        writer.write("一车间4#磨机台效" + resultSet.getDouble("一车间4#磨机台效") + "，");
                        writer.write("一车间4#磨机运转率" + resultSet.getDouble("一车间4#磨机运转率"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                // 关闭数据库连接
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}